package xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import target.Word;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;

/**
 *从XML文件中得到符合条件的单词，返回一个List<Word> 中，搜索方法用到了三层过滤的方法
 * @author GeH
 */
public class WordSearching {

    public WordSearching() {
    }

    //第一层，从XML文件中筛选出满足status条件的单词
    private List<Word> wordSearchingStatus(String status) {
        
        List<Word> wordList = new ArrayList<Word>();
        
         try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File("dic/UserList.xml"));

            List list = document.selectNodes("/wordbook/item");
            Iterator iter = list.iterator();
     
            while (iter.hasNext()) {
                Element itemElement = (Element) iter.next();
                Iterator iterator = itemElement.elementIterator("status");
                while (iterator.hasNext()) {
                    Element statusElement = (Element) iterator.next();
                    if(statusElement.getText().equals(status) || status == "")
                    {
                        Word word = new Word();
                        if (statusElement.getText().equals("0"))
                        {
                            word.status = word.status.DUMP;
                        }
                        if ( statusElement.getText().equals("1"))
                        {
                            word.status = word.status.REM;
                        }
                        if ( statusElement.getText().equals("2") )
                        {
                            word.status = word.status.NREM;
                        }
                            
                         iterator = itemElement.elementIterator("word");
                        while (iterator.hasNext()) {
                            Element wordElement = (Element) iterator.next();
                            word.word = wordElement.getText();
                        }

                        iterator = itemElement.elementIterator("rating");
                        while (iterator.hasNext()) {
                            Element rateElement = (Element)iterator.next();
                            word.rating =  Integer.parseInt(rateElement.getText());
                        }

                        iterator = itemElement.elementIterator("content");
                        while (iterator.hasNext()) {
                            Element contentElement = (Element) iterator.next();
                            word.content = contentElement.getText();
                        }

                        iterator = itemElement.elementIterator("tricks");
                        while (iterator.hasNext()) {
                            Element tricksElement = (Element) iterator.next();
                            word.tricks = tricksElement.getText();
                        }

                        iterator = itemElement.elementIterator("date");
                        while (iterator.hasNext()) {
                            Element dateElement = (Element) iterator.next();
                            word.date = dateElement.getText();
                        }       
                        wordList.add(word);
                    }
                }

            }
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
         
         return wordList;
    }
    
    //第二层，再筛选出满足rating范围的单词
     private List<Word> wordSearchingRating(String ratingDown,String ratingUp,List<Word> wordListSearching ) {
         List<Word> wordList =  new ArrayList<Word>();
         for(int i = 0 ;i<wordListSearching.size();i++) 
         {
             if(ratingDown == "")
             {
                 ratingDown = "1";
             }
             
             if (ratingUp == "")
             {
                 ratingUp = "5";
             }
             if ( wordListSearching.get(i).rating>=Integer.parseInt(ratingDown)
                  && wordListSearching.get(i).rating <= Integer.parseInt(ratingUp) )
             {
                 wordList.add(wordListSearching.get(i));
             }
         }
         return wordList;
     }
     //第三层，在筛选出满足date范围的单词
     private List<Word> wordSearchingDate(String dateDown,String dateUp,List<Word> wordListdate ) {
         List<Word> wordList =  new ArrayList<Word>();
         for(int i = 0 ;i<wordListdate.size();i++) 
         {
             if(dateDown == "")
             {
                 dateDown = "19900101";
             }
             
             if (dateUp == "")
             {
                 dateUp = "20500101";
             }
             if ( Integer.parseInt(wordListdate.get(i).date)>=Integer.parseInt(dateDown)
                  && Integer.parseInt(wordListdate.get(i).date) <= Integer.parseInt(dateUp) )
             {
                 wordList.add(wordListdate.get(i));
             }
         }
         return wordList;
     }
     
     public List<Word> wordSearching( String status,String ratingDown,String ratingUp,String dateDown,String dateUp )
     {
         List<Word> wordListStatus = new ArrayList<Word>();
         wordListStatus = wordSearchingStatus(status);
         List<Word> wordListRating = new ArrayList<Word>();
         wordListRating = wordSearchingRating( ratingDown,ratingUp,wordListStatus);
         List<Word> wordList = new ArrayList<Word>();
         wordList = wordSearchingDate (dateDown,dateUp,wordListRating);
         return wordList; 
     }
}
